<!-- 发布 -->
<template>
	<view>
		<u-sticky offsetTop="-20">
			<view id="need_head">
				<view id="search" @click="gotosearch()">
					<u-search :show-action="false" :clearabled="false" shape="round" bg-color="#fff" :disabled="true">
					</u-search>
				</view>
			</view>
		</u-sticky>
		<view id="need">
			<view class="need-card" v-for="(item, index) in need_card_list" :key="index">
				<view @click="getneedicardinfo(item.demandUuid)">
					<needcard :needcardall="item"></needcard>
				</view>
			</view>
		</view>
		<u-divider v-if="inbottom" color="#878787" bg-color="#f5f5f5" margin-top="60" margin-bottom="60">到底了</u-divider>
		<u-back-top :scroll-top="scrollTop" mode="square" tips="顶部"></u-back-top>
	</view>
</template>

<script>
	import needcard from '../components/needcard/needcard.vue';
	export default {
		components:{
			needcard
		},
		data() {
			return {
				//回到顶部
				pagenum: 0,
				scrollTop: 0,
				need_card_list: [],
				cardshow:0,
				pageSize: 10,//分页
				inbottom: false
			}
		},
		methods: {
			//卡片点击
			getneedicardinfo(uuid){
				// console.log(uuid);
				let url = '../subpages/Details/demand_details?uuid='+uuid;
				this.goto(url)
			},
			//页面跳转
			goto(url) {
				uni.navigateTo({
					url: url,
					animationType: 'slide-in-right',
					animationDuration: 1000
				})
			},
			gotosearch() {
				uni.navigateTo({
					url: '../subpages/search/search',
					animationType: 'fade-in',
					animationDuration: 500
			
				})
			},
			pushlist(list){
				// let index = this.$u.random(0, list.length - 1);
				// let index=0;
				for (let i = 0; i < list.length; i++) {
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(list[i]))
					// item.id = this.$u.guid();
					this.need_card_list.push(item);
				}
			},
			getData() {
				this.$myRequest({
					url: '/demand/getNeedGoods',
					method: 'POST',
					data: {
						'pageNum': this.pagenum,
						'pageSize':this.pageSize
					}
				}).then(res=>{
					if(res.data.code===200){
						// console.log(res);
						if(res.data.data.records.length === 0){
							this.inbottom = true;
							return 0;
						} 
						this.pushlist(res.data.data.records);
						this.pagenum++;
					}
					
				})
				
			}
				
		},
		onLoad() {
			this.getData();
		},
		onPageScroll(e) { //页面滚动
			this.scrollTop = e.scrollTop
		},
		onPullDownRefresh() {
			//监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
			this.need_card_list = [];
			this.getData();
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止下拉刷新动画
			}, 500);
		},
		onReachBottom() { //到达底部
			this.getData();
		}
	}
</script>

<style>
	@import url("need.css");
</style>
